import axios from "axios";
import router from "@/router/index";

const request = axios.create({
    baseURL: '/api',
    timeout: 600000
})

// 响应拦截器
try {
    request.interceptors.response.use(
        (response) => {
            return response.data
        },
        (error) => {
            if (error.response.status == 401) {
                // ElMessage.error('');
                router.push('/login')
                alert('登录超时，请重新登录')
            } else {
                alert('接口访问异常')
            }
            return Promise.reject(error)
        }
    )
} catch (e) {
    console.log(e)
}

// 请求拦截器
request.interceptors.request.use(
    (config) => {
        const loginUser = JSON.parse(localStorage.getItem('loginUser'))
        if (loginUser && loginUser.token) {
            config.headers.token = loginUser.token
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)


export default request